import {ref, DefineComponent} from "vue"
import guilingfunc from "../util/guiling";

export default () => {
    // 表单数据
    const formValue = ref<{ [key: string]: any }>({})
    // 表单组件
    const formRef = ref<DefineComponent | null>(null)
    // 分页组件
    const pageRef = ref<DefineComponent | null>(null)
    // 编辑弹窗组价
    const editRef = ref<DefineComponent | null>(null)
    // 表格数据
    const tableData = ref<Array<any>>([])
    // 数据总数
    const total = ref<number>(0)
    // 表格用的loading状态
    const loading = ref<boolean>(false)
    // 点击查询
    const handleSearch = () => {
        console.log(pageRef.value,"分页组件");
        pageRef.value && pageRef.value.changePage({page: 1})
    }
    // 点击重置
    const handleResetForm = () => {
        formValue.value = guilingfunc(formValue.value)
    }
    // 点击查看
    const handleView = (data: any) => {
        editRef.value!.handleInit("view", {...data})
    }
    // 点击编辑
    const handleEdit = (data: any) => {
        editRef.value!.handleInit("edit", {...data})
    }
    // 点击新建
    const handleCreate = () => {
        editRef.value!.handleInit("add", {})
    }
    // 删除后的刷新
    const refreshDelete = (type = "delete") => {
        if (type === "add" || type === "") {
            pageRef.value && pageRef.value.changePage({page: 1})
        } else if (type === "edit" || type === "view") {
            pageRef.value && pageRef.value.changePage({})
        } else {
            const page = pageRef.value ? pageRef.value.page : 1
            if (page > 1 && tableData.value.length === 1) {
                pageRef.value && pageRef.value.changePage({page: page - 1})
            }
            pageRef.value && pageRef.value.changePage({})
        }
    }
    return {
        formValue,
        loading,
        total,
        tableData,
        editRef,
        pageRef,
        formRef,
        handleSearch,
        handleResetForm,
        handleView,
        handleEdit,
        handleCreate,
        refreshDelete
    }
}
